package com.cc.wechat.controller;

import com.cc.wechat.common.ApiResponse;
import com.cc.wechat.common.constants.MessageConstants;
import com.cc.wechat.dto.request.PrivateMessageRequest;
import com.cc.wechat.entity.PrivateMessage;
import com.cc.wechat.service.MessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    /**
     * 发送私聊消息
     */
    @PostMapping("/private")
    public ApiResponse<PrivateMessage> sendPrivateMessage(@RequestBody @Validated PrivateMessageRequest request) {
        PrivateMessage message = messageService.sendPrivateMessage(request);
        return ApiResponse.success(MessageConstants.Message.SEND_SUCCESS, message);
    }

    /**
     * 获取与指定用户的聊天记录
     */
    @GetMapping("/private/{friendId}")
    public ApiResponse<Page<PrivateMessage>> getPrivateMessages(
            @PathVariable Long friendId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        return ApiResponse.success(messageService.getPrivateMessages(friendId, page, size));
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/private/read/{friendId}")
    public ApiResponse<Void> markMessagesAsRead(@PathVariable Long friendId) {
        messageService.markMessagesAsRead(friendId);
        return ApiResponse.success(null);
    }

    /**
     * 撤回消息
     */
    @DeleteMapping("/private/{messageId}")
    public ApiResponse<Void> recallMessage(@PathVariable Long messageId) {
        messageService.recallMessage(messageId);
        return ApiResponse.success(null);
    }
} 